package com.sx.micowell.framework.tools;

import android.content.Context;
import android.os.Environment;

import androidx.annotation.NonNull;

import java.io.File;

/**
 * SD卡相关工具类
 */
public class SDCardUtil {

    /**
     * 获取应用默认缓存路径
     *
     * @param context 上下文
     */
    public static String getDefaultPath(@NonNull Context context) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + context.getPackageName();
        }
        boolean useExternalStorage = false;
        if (context.getExternalCacheDir() != null) {
            if (Environment.getExternalStorageState().equals("mounted")) {
                if (Environment.getExternalStorageDirectory().getFreeSpace() > 0) {
                    useExternalStorage = true;
                }
            }
        }
        if (useExternalStorage) {
            return context.getExternalCacheDir().getAbsolutePath();
        } else {
            return context.getCacheDir().getAbsolutePath();
        }
    }

}